﻿using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
namespace WPFExcelReport
{
    public class CaseInsensitiveComparer : IComparer<object>
    {
        public int Compare(object x, object y)
        {
            return string.Compare((string)x, (string)y, StringComparison.OrdinalIgnoreCase);
        }
    }

    public partial class ITXEXRPT
    {
        private void YCACOFS(DataSet db)
        {
            try
            {
                string value = "";
                DataRow row = null;
                decimal colorqty = 0;
                decimal VMQPLANPEK = 0;
                decimal VMQSTDPEK = 0;
                decimal VMQACTPEK = 0;
                decimal PRODV = 0;
                decimal totalqty = 0;
                decimal TVMQSTDPEK = 0;
                decimal TVMQACTPEK = 0;
                decimal TVMQPLANPEK = 0;
                int x = 1, y = 1;
                x = 5; y = 14;
                row = db.Tables["ABKOPF"].Rows[0];
                value = row["ABKAUFKRS"].ToString() + "-" + row["ABKAUFLFD"].ToString();
                exAgent.SetCellValue(x, y, value);

                x = 3; y = 30;
                exAgent.SetCellValue(1, 31, date);
                int rown = -1;
                string processed = "";
                int posx = 18;
                int rpl = 9;
                int count = db.Tables["ICC_ABPOS_MCO"].Rows.Count;
                foreach (DataRow dwABCO in db.Tables["ICC_ABPOS_MCO"].Rows)
                {
                    exAgent.SetCellValue(posx, 1, "颜色"); exAgent.SetCellValue(posx, 6, "加工" + Environment.NewLine + "费用");
                    exAgent.SetCellValue(posx, 11, "加工" + Environment.NewLine + "单价");
                    exAgent.SetCellValue(posx, 14, "实际原料" + Environment.NewLine + "费用");
                    exAgent.SetCellValue(posx, 21, "总件数");
                    exAgent.SetCellValue(posx, 24, "尺码数量");
                    exAgent.SetCellValue(posx + 1, 24, "S"); exAgent.SetCellValue(posx + 1, 27, "M"); exAgent.SetCellValue(posx + 1, 30, "L");
                    exAgent.SetCellValue(posx + 1, 33, "XL");
                    exAgent.SetCellValue(posx + 4, 1, "总费用(加工费+原料费):");
                    exAgent.SetCellValue(posx + 4, 24, "单价(元/件):");
                    exAgent.SetCellValue(posx + 5, 1, "物料清单");
                    exAgent.SetCellValue(posx + 6, 1, "类型");
                    exAgent.SetCellValue(posx + 6, 3, "物料");
                    exAgent.SetCellValue(posx + 6, 10, "颜色/尺码");
                    exAgent.SetCellValue(posx + 6, 15, "标准量");
                    exAgent.SetCellValue(posx + 6, 19, "计划量");
                    exAgent.SetCellValue(posx + 6, 23, "实际量");
                    exAgent.SetCellValue(posx + 6, 27, "采购单价");
                    exAgent.SetCellValue(posx + 6, 31, "采购单");


                    string art = dwABCO["ART"].ToString();
                    string var = dwABCO["VAR"].ToString();
                    var = Left(var, 2);
                    string color = dwABCO["FARBE"].ToString();

                    if (rown == -1)
                    {
                        string enddate = dwABCO["ENDDATE"].ToString();  
                        exAgent.SetCellValue(2, 21, enddate);//加工完成日期
                        string APPDATE = dwABCO["APPDATE"].ToString(); 
                        exAgent.SetCellValue(3, 31, APPDATE);//评估日期:          
                        x = 3; y = 4;
                        exAgent.SetCellValue(x, y, art);
                        x = 3; y = 9;
                        exAgent.SetCellValue(x, y, var);
                        value = "品名:" + GetGB2312Value(dwABCO["ARTDES"].ToString());
                        x = 3; y = 14;
                        exAgent.SetCellValue(x, y, value);
                    }

                    #region====Color===================

                    if (!processed.Contains(color + ";"))
                    {
                        processed += color + ";";
                        rown++;
                        x = posx + 2; y = 1;
                        exAgent.SetCellValue(x, y, color);
                        value = GetGB2312Value(dwABCO["CDES"].ToString());
                        x = posx + 2 + 1; y = 1;
                        exAgent.SetCellValue(x, y, value);
                    }
                    exAgent.SetCellValue(posx + 2, 24, dwABCO["S"].ToString());
                    exAgent.SetCellValue(posx + 2, 27, dwABCO["M"].ToString());
                    exAgent.SetCellValue(posx + 2, 30, dwABCO["L"].ToString());
                    exAgent.SetCellValue(posx + 2, 33, dwABCO["XL"].ToString());
                    exAgent.SetCellValue(posx + 2 + 1, 24, dwABCO["SP"].ToString());
                    exAgent.SetCellValue(posx + 2 + 1, 27, dwABCO["MP"].ToString());
                    exAgent.SetCellValue(posx + 2 + 1, 30, dwABCO["LP"].ToString());
                    exAgent.SetCellValue(posx + 2 + 1, 33, dwABCO["XLP"].ToString());
                    #endregion
                    PRODV = System.Convert.ToDecimal(dwABCO["PRODV"].ToString());
                    exAgent.SetCellValue(posx + 2, 6, PRODV);
                    exAgent.SetCellValue(posx + 2, 11, Convert.ToDouble(dwABCO["MPRICE"].ToString()));
                    VMQPLANPEK = System.Convert.ToDecimal(dwABCO["VMQPLANPEK"].ToString());
                    VMQSTDPEK = System.Convert.ToDecimal(dwABCO["VMQSTDPEK"].ToString());
                    VMQACTPEK = System.Convert.ToDecimal(dwABCO["VMQACTPEK"].ToString());
                    exAgent.SetCellValue(posx + 2, 14, VMQACTPEK);
                    //exAgent.SetCellValue(posx + 2, 18, "实际原料");

                    exAgent.SetCellValue(posx + 5, 5, "标准原料");
                    exAgent.SetCellValue(posx + 5, 10, VMQSTDPEK);  
                    exAgent.SetCellValue(posx + 5, 15, "计划原料");
                    exAgent.SetCellValue(posx + 5, 19, VMQPLANPEK);
                    exAgent.SetCellValue(posx + 5, 23, "实际原料");
                    exAgent.SetCellValue(posx + 5, 27, VMQACTPEK);
                    
                    exAgent.SetCellValue(posx + 2, 21, dwABCO["QTY"].ToString());
                    exAgent.SetCellValue(posx + 2 + 1, 21, dwABCO["QTYP"].ToString());
                    colorqty = System.Convert.ToDecimal(dwABCO["QTY"].ToString());

                    exAgent.SetCellValue(posx + 2 + 2, 18, System.Math.Round((VMQACTPEK + PRODV), 0));
                    if (colorqty > 0)
                    {
                        exAgent.SetCellValue(posx + 2 + 2, 30, System.Math.Round((VMQACTPEK + PRODV) / colorqty, 2));
                    }
                    totalqty += colorqty;
                    TVMQPLANPEK += VMQPLANPEK + PRODV;
                    TVMQSTDPEK += VMQSTDPEK + PRODV;
                    TVMQACTPEK += VMQACTPEK + PRODV;
                    posx = FillInXDECK(posx + rpl - 2, 1, dwABCO,db);
                    posx++;
                    if (rown < count - 1)
                    {
                        exAgent.RangeCopy(18, 18 + rpl - 1, posx, posx + rpl - 1);
                    }
                }
                if (totalqty > 0)
                {
                    exAgent.SetCellValue(5, 6, System.Math.Round(TVMQSTDPEK / totalqty, 2));
                    exAgent.SetCellValue(5, 16, System.Math.Round(TVMQPLANPEK / totalqty, 2));
                    exAgent.SetCellValue(5, 26, System.Math.Round(TVMQACTPEK / totalqty, 2));
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private int FillInXDECK(int posx, int starty, DataRow dwABCO,DataSet db)
        {
            int rown = -1;
            int x = posx;
            int y = starty;
            int rpl = 2;
            try
            {
                DataTable tb = db.Tables["ICC_ABAUFM"];
                var query = (from rw in tb.AsEnumerable()
                             where rw["ABPART"].ToString() == dwABCO["ART"].ToString()
                             && rw["ABPVAR"].ToString().Trim() == dwABCO["VAR"].ToString().Trim()
                             && rw["ABPFARBE"].ToString() == dwABCO["FARBE"].ToString()
                             group rw by new
                             {

                                 MTYPE = rw["MTYPE"].ToString(),
                                 ART = rw["ABAART"].ToString(),
                                 VAR = rw["ABAVAR"].ToString(),
                                 COLOR = rw["ABAFARBE"].ToString(),
                                 ASIZE = rw["ABAGROE"].ToString(),
                                 UM = rw["WAUEBM"].ToString(),
                                 FGRLFD = rw["WAFGRLFD"].ToString(),
                                 
                             }
                                 into g
                                 select new
                                 {
                                     KEY = g.Key,
                                     ARTDES = g.Max(rw => rw.Field<string>("ARTDES")),
                                     CDES = g.Max(rw => rw.Field<string>("CDES")),
                                     EKPAUF = g.Max(rw => (rw.Field<string>("ABAAUFKRS") + "-" + Helpers.Helper.GetSubRollNum(rw.Field<string>("ABAAUFLFD")) + "-" + Helpers.Helper.GetSubRollNum(rw.Field<string>("ABAAUFPOS")))),
                                     QTYPLAN = g.Sum(rw => rw.Field<decimal>("QTYPLAN")),
                                     QTYACT = g.Sum(rw => rw.Field<decimal>("QTYACT")),
                                     QTYSTD = g.Sum(rw => rw.Field<decimal>("QTYSTD")),
                                     PRSTD = g.Max(rw => rw.Field<decimal>("PRSTD")),
                                     PREK = g.Max(rw => rw.Field<decimal>("PREK")),
                                     PRPLAN = g.Max(rw => rw.Field<decimal>("PRPLAN")),
                                     RATIO = g.Max(rw => rw.Field<decimal>("RATIO")),
                                     VQTYSTDPRSTD = g.Sum(rw => rw.Field<decimal>("VQTYSTDPRSTD")),
                                     VQTYSTDPREK = g.Sum(rw => rw.Field<decimal>("VQTYSTDPREK")),
                                     VQTYSTDPRPLAN = g.Sum(rw => rw.Field<decimal>("VQTYSTDPRPLAN")),
                                     VQTYPLANPRSTD = g.Sum(rw => rw.Field<decimal>("VQTYPLANPRSTD")),
                                     VQTYPLANPREK = g.Sum(rw => rw.Field<decimal>("VQTYPLANPREK")),
                                     VQTYPLANPRPLAN = g.Sum(rw => rw.Field<decimal>("VQTYPLANPRPLAN")),
                                     VQTYACTPRSTD = g.Sum(rw => rw.Field<decimal>("VQTYACTPRSTD")),
                                     VQTYACTPREK = g.Sum(rw => rw.Field<decimal>("VQTYACTPREK")),
                                     VQTYACTPRPLAN = g.Sum(rw => rw.Field<decimal>("VQTYACTPRPLAN")),
                                 }).OrderBy(
                                 b => b.KEY.MTYPE, new CaseInsensitiveComparer())
                                 .ThenBy(b => b.KEY.ART, new CaseInsensitiveComparer())
                                .ThenBy(b => b.KEY.VAR, new CaseInsensitiveComparer())
                                .ThenBy(b => b.KEY.COLOR, new CaseInsensitiveComparer())
                                .ThenBy(b => b.KEY.ASIZE, new CaseInsensitiveComparer()); ;

                int count = query.Count();
                foreach (var rw in query)
                {
                    rown++;
                    string art = rw.KEY.ART.ToString();
                    string var = Left(rw.KEY.VAR.ToString() + "   ", 2);
                    string color = rw.KEY.COLOR.ToString();
                    double RT = 1;
                    if (rw.KEY.UM.Trim() == "KG")
                    {
                        //RT = Convert.ToDouble(rw.KEY.FGRLFD) * 0.001;
                    }
                    exAgent.SetCellValue(x + rown * rpl, 1, rw.KEY.MTYPE);
                    exAgent.SetCellValue(x + rown * rpl, 3, rw.KEY.ART.ToString() + rw.KEY.VAR.ToString());
                    exAgent.SetCellValue(x + rown * rpl + 1, 3, GetGB2312Value(rw.ARTDES));
                    if (rw.KEY.ASIZE.ToString().Trim() == "")
                    {
                        exAgent.SetCellValue(x + rown * rpl, 10, rw.KEY.COLOR.Trim());
                    }
                    else
                    {
                        exAgent.SetCellValue(x + rown * rpl, 10, rw.KEY.COLOR.Trim() + "/" + rw.KEY.ASIZE);
                    }
                    exAgent.SetCellValue(x + rown * rpl + 1, 10, GetGB2312Value(rw.CDES));
                    exAgent.SetCellValue(x + rown * rpl + 1, 15, "单耗:"+rw.RATIO);
                    exAgent.SetCellValue(x + rown * rpl, 31, rw.EKPAUF);

                    exAgent.SetCellValue(x + rown * rpl, 15, Math.Round(Convert.ToDouble(rw.QTYSTD) * RT,0));
                    exAgent.SetCellValue(x + rown * rpl, 19, Math.Round(Convert.ToDouble(rw.QTYPLAN) * RT,0));
                    exAgent.SetCellValue(x + rown * rpl, 23, Math.Round(Convert.ToDouble(rw.QTYACT) * RT,0));
                    exAgent.SetCellValue(x + rown * rpl, 27, Math.Round(Convert.ToDouble(rw.PREK),2));
                    if (rown < count - 1)
                    {
                        exAgent.RangeCopy(posx, posx + rpl - 1, posx + (rown + 1) * rpl, posx + (rown + 1) * rpl + rpl);
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return posx + (rown + 1) * rpl;
        }
    }
}